package com.yunpeng.aidevice.dao.service.impl;

import com.yunpeng.aidevice.dao.constant.DictionariesConstant;
import com.yunpeng.aidevice.dao.mapper.record.DictionariesMapper;
import com.yunpeng.aidevice.dao.model.record.DictionariesDO;
import com.yunpeng.aidevice.dao.service.IDictionariesService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * DictionariesServiceImpl 字典数据服务
 *
 * @author : Moore(hemw)
 * @date : 2020-04-13
 */
@Service
public class DictionariesServiceImpl implements IDictionariesService {


    @Resource
    private DictionariesMapper dictionariesMapper;


    @Override
    /**
     * 根据业务域和字典组查询字典数据集合
     *
     * @param domain 业务域
     * @param group  字典组
     * @return
     */
    public List<DictionariesDO> queryDictionariesList(String domain, String group) {
        if (StringUtils.isBlank(domain)) {
            return null;
        }
        if (StringUtils.isBlank(group)) {
            return null;
        }
        Map<String, Object> queryMap = new HashMap<>(16);
        queryMap.put("flag", 1);
        queryMap.put("key_domain", domain);
        queryMap.put("key_group", group);
        return dictionariesMapper.queryForList(queryMap);
    }

    @Override
    /**
     * 根据组集合获取字典集合
     *
     * @param domain 域
     * @param groups 组集合
     * @return
     */
    public List<DictionariesDO> getByGroupList(String domain, List<String> groups) {
        if (StringUtils.isBlank(domain)) {
            return null;
        }
        if (groups == null || groups.isEmpty()) {
            return null;
        }
        return dictionariesMapper.getByGroupList(domain, groups);
    }


    @Override
    /**
     * 更新字典
     *
     * @param domain 域
     * @param group  组
     * @param key    键
     * @param value  值
     * @return
     */
    public Boolean updateDictionaries(String domain, String group, String key, String value) {
        if (StringUtils.isBlank(domain)) {
            return false;
        }
        if (StringUtils.isBlank(group)) {
            return false;
        }
        if (StringUtils.isBlank(key)) {
            return false;
        }
        if (StringUtils.isBlank(value)) {
            return false;
        }
        dictionariesMapper.updateByKey(domain, group, key, value);
        //更新本地缓存
        DictionariesConstant.INSTANCE.clearCache();
        return true;
    }

    @Override
    /**
     * 获取服务器序号列表
     *
     * @return
     */
    public List<String> getServerMachineNumberList() {
        Map<String, Object> queryMap = new HashMap<>(16);
        queryMap.put("flag", 1);
        queryMap.put("key_domain", "server");
        queryMap.put("key_group", "machine");
        queryMap.put("key_name", "numberList");
        List<DictionariesDO> dictionariesDOList = dictionariesMapper.queryForList(queryMap);
        if (dictionariesDOList == null || dictionariesDOList.isEmpty()) {
            return null;
        } else {
            return Arrays.asList(dictionariesDOList.get(0).getKeyValue().split(","));
        }
    }


    @Override
    /**
     * 获取系统默认用户代码
     *
     * @return
     */
    public String getDefaultUserCode() {
        Map<String, Object> queryMap = new HashMap<>(16);
        queryMap.put("flag", 1);
        queryMap.put("key_domain", "system");
        queryMap.put("key_group", "config");
        queryMap.put("key_name", "defaultUserCode");
        List<DictionariesDO> dictionariesDOList = dictionariesMapper.queryForList(queryMap);
        if (dictionariesDOList == null || dictionariesDOList.isEmpty()) {
            return null;
        } else {
            return dictionariesDOList.get(0).getKeyValue();
        }
    }

    @Override
    /**
     * 获取用户登录后默认角色
     *
     * @return
     */
    public String getSystemDefaultRole() {
        Map<String, Object> queryMap = new HashMap<>(16);
        queryMap.put("flag", 1);
        queryMap.put("key_domain", "system");
        queryMap.put("key_group", "role");
        queryMap.put("key_name", "default");
        List<DictionariesDO> dictionariesDOList = dictionariesMapper.queryForList(queryMap);
        if (dictionariesDOList == null || dictionariesDOList.isEmpty()) {
            return null;
        } else {
            return dictionariesDOList.get(0).getKeyValue();
        }
    }

    @Override
    /**
     * 获取系统运维管理员权限
     *
     * @return
     */
    public String getSystemAdminRole() {
        Map<String, Object> queryMap = new HashMap<>(16);
        queryMap.put("flag", 1);
        queryMap.put("key_domain", "system");
        queryMap.put("key_group", "role");
        queryMap.put("key_name", "admin");
        List<DictionariesDO> dictionariesDOList = dictionariesMapper.queryForList(queryMap);
        if (dictionariesDOList == null || dictionariesDOList.isEmpty()) {
            return null;
        } else {
            return dictionariesDOList.get(0).getKeyValue();
        }
    }

    @Override
    /**
     * 系统业务运营管理员
     *
     * @return
     */
    public String getSystemOperateAdminRole() {
        Map<String, Object> queryMap = new HashMap<>(16);
        queryMap.put("flag", 1);
        queryMap.put("key_domain", "system");
        queryMap.put("key_group", "role");
        queryMap.put("key_name", "operateAdmin");
        List<DictionariesDO> dictionariesDOList = dictionariesMapper.queryForList(queryMap);
        if (dictionariesDOList == null || dictionariesDOList.isEmpty()) {
            return null;
        } else {
            return dictionariesDOList.get(0).getKeyValue();
        }
    }

    @Override
    /**
     * 系统默认租户代码
     *
     * @return
     */
    public String getSystemDefaultTenantCode() {
        Map<String, Object> queryMap = new HashMap<>(16);
        queryMap.put("flag", 1);
        queryMap.put("key_domain", "system");
        queryMap.put("key_group", "config");
        queryMap.put("key_name", "defaultTenantCode");
        List<DictionariesDO> dictionariesDOList = dictionariesMapper.queryForList(queryMap);
        if (dictionariesDOList == null || dictionariesDOList.isEmpty()) {
            return null;
        } else {
            return dictionariesDOList.get(0).getKeyValue();
        }
    }

    @Override
    /**
     * 设备通知规则名
     *
     * @return
     */
    public String getSystemDeviceNoticeRuleName() {
        Map<String, Object> queryMap = new HashMap<>(16);
        queryMap.put("flag", 1);
        queryMap.put("key_domain", "system");
        queryMap.put("key_group", "deviceNoticeConfig");
        queryMap.put("key_name", "ruleName");
        List<DictionariesDO> dictionariesDOList = dictionariesMapper.queryForList(queryMap);
        if (dictionariesDOList == null || dictionariesDOList.isEmpty()) {
            return null;
        } else {
            return dictionariesDOList.get(0).getKeyValue();
        }
    }

    @Override
    /**
     * 设备通知参数名
     *
     * @return
     */
    public String getSystemDeviceNoticeParameterName() {
        Map<String, Object> queryMap = new HashMap<>(16);
        queryMap.put("flag", 1);
        queryMap.put("key_domain", "system");
        queryMap.put("key_group", "deviceNoticeConfig");
        queryMap.put("key_name", "temperature");
        List<DictionariesDO> dictionariesDOList = dictionariesMapper.queryForList(queryMap);
        if (dictionariesDOList == null || dictionariesDOList.isEmpty()) {
            return null;
        } else {
            return dictionariesDOList.get(0).getKeyValue();
        }
    }

    @Override
    /**
     * 设备通知默认温度值
     *
     * @return
     */
    public Double getSystemDefaultNoticeTemp() {
        Map<String, Object> queryMap = new HashMap<>(16);
        queryMap.put("flag", 1);
        queryMap.put("key_domain", "system");
        queryMap.put("key_group", "config");
        queryMap.put("key_name", "deviceNoticeTemp");
        List<DictionariesDO> dictionariesDOList = dictionariesMapper.queryForList(queryMap);
        if (dictionariesDOList == null || dictionariesDOList.isEmpty()) {
            return null;
        } else {
            return Double.parseDouble(dictionariesDOList.get(0).getKeyValue());
        }
    }

    @Override
    /**
     * 德沃数据上传接口地址
     *
     * @return
     */
    public String getDwUploadDataApi() {
        Map<String, Object> queryMap = new HashMap<>(16);
        queryMap.put("flag", 1);
        queryMap.put("key_domain", "system");
        queryMap.put("key_group", "config");
        queryMap.put("key_name", "dwUploadPath");
        List<DictionariesDO> dictionariesDOList = dictionariesMapper.queryForList(queryMap);
        if (dictionariesDOList == null || dictionariesDOList.isEmpty()) {
            return null;
        } else {
            return dictionariesDOList.get(0).getKeyValue();
        }
    }

    @Override
    /**
     * 限定的文件后缀
     *
     * @return
     */
    public String getFileAllowSuffix() {
        Map<String, Object> queryMap = new HashMap<>(16);
        queryMap.put("flag", 1);
        queryMap.put("key_domain", "system");
        queryMap.put("key_group", "config");
        queryMap.put("key_name", "fileAllowSuffix");
        List<DictionariesDO> dictionariesDOList = dictionariesMapper.queryForList(queryMap);
        if (dictionariesDOList == null || dictionariesDOList.isEmpty()) {
            return null;
        } else {
            return dictionariesDOList.get(0).getKeyValue();
        }
    }

    @Override
    /**
     * 最大文件大小
     *
     * @return
     */
    public Long getMaxFileSize() {
        Map<String, Object> queryMap = new HashMap<>(16);
        queryMap.put("flag", 1);
        queryMap.put("key_domain", "system");
        queryMap.put("key_group", "config");
        queryMap.put("key_name", "maxFileSize");
        List<DictionariesDO> dictionariesDOList = dictionariesMapper.queryForList(queryMap);
        if (dictionariesDOList == null || dictionariesDOList.isEmpty()) {
            return null;
        } else {
            return Long.parseLong(dictionariesDOList.get(0).getKeyValue());
        }
    }

    @Override
    /**
     * 文件保存地址
     *
     * @return
     */
    public String getFileSavePath() {
        Map<String, Object> queryMap = new HashMap<>(16);
        queryMap.put("flag", 1);
        queryMap.put("key_domain", "system");
        queryMap.put("key_group", "config");
        queryMap.put("key_name", "fileSavePath");
        List<DictionariesDO> dictionariesDOList = dictionariesMapper.queryForList(queryMap);
        if (dictionariesDOList == null || dictionariesDOList.isEmpty()) {
            return null;
        } else {
            return dictionariesDOList.get(0).getKeyValue();
        }
    }

    @Override
    /**
     * 文件访问地址
     *
     * @return
     */
    public String getFileAccessUrl() {
        Map<String, Object> queryMap = new HashMap<>(16);
        queryMap.put("flag", 1);
        queryMap.put("key_domain", "system");
        queryMap.put("key_group", "config");
        queryMap.put("key_name", "fileAccessUrl");
        List<DictionariesDO> dictionariesDOList = dictionariesMapper.queryForList(queryMap);
        if (dictionariesDOList == null || dictionariesDOList.isEmpty()) {
            return null;
        } else {
            return dictionariesDOList.get(0).getKeyValue();
        }
    }


}
